/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.grid;

import com.blandware.atleap.webapp.taglib.core.util.TaglibConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

/**
 * <p>Marks some area as grid's column. <br />
 * This tag is optional, it can be used to specify one <em>fieldName</em> and
 * <em>fieldKey</em> for all grid tags which are nested within column.
 * <br />
 * This tag is only valid when nested within <em>grid</em> tag.
 * </p>
 * <p>
 * Allowed attributes are:
 * <ul>
 * <li>
 * <b>fieldName</b> - required - name of field that will be assinged to this
 * column
 * </li>
 * <li>
 * <b>fieldKey</b> - bundle key for localized field name
 * </li>
 * <li>
 * <b>nameVar</b> - name of variable that will store field name. If not
 * spesified, this will be <code>TaglibConstants.FIELD_NAME_ATTR</code>.
 * </li>
 * <li>
 * <b>keyVar</b> - name of variable that will store field key. If not specified,
 * this will be <code>TaglibConstants.FIELD_KEY_ATTR</code>.
 * </li>
 * </ul>
 * </p>
 * <p><a href="ColumnTag.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.9 $ $Date: 2006/08/03 10:08:18 $
 * @jsp.tag name="column"
 * body-content="scriptless"
 */
public class ColumnTag extends SimpleTagSupport {

	protected transient final Log log = LogFactory.getLog(ColumnTag.class);

	/**
	 * Name of field in this column
	 */
	protected String fieldName = null;

	/**
	 * Bundle key for this field name
	 */
	protected String fieldKey = null;

	/**
	 * Name of page scope attribute, under which fieldName will be saved
	 */
	protected String nameVar = TaglibConstants.FIELD_NAME_ATTR;

	/**
	 * Name of page scope attribute under which fieldKey will be saved
	 */
	protected String keyVar = TaglibConstants.FIELD_KEY_ATTR;


	/**
     * Returns name of field in this column
     *
	 * @return field name
	 * @jsp.attribute required="true"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Name of field in this column"
	 */
	public String getFieldName() {
		return fieldName;
	}

    /**
     * Sets name of field in this column
     *
     * @param fieldName field name
     */
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	/**
     * Returns bundle key for field name
     *
	 * @return bundle key
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Bundle key for this field name"
	 */
	public String getFieldKey() {
		return fieldKey;
	}

    /**
     * Sets bundle key for field name
     *
     * @param fieldKey bundle key
     */
	public void setFieldKey(String fieldKey) {
		this.fieldKey = fieldKey;
	}

	/**
     * Returns name of page scope attribute, under which field name will be saved
     *
	 * @return name of page scope attribute, under which field name will be saved
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Name of page scope attribute, under which fieldName will be saved"
	 */
	public String getNameVar() {
		return nameVar;
	}

    /**
     * Sets name of page scope attribute, under which field name will be saved
     *
     * @param nameVar name of page scope attribute, under which field name will be saved
     */
	public void setNameVar(String nameVar) {
		this.nameVar = nameVar;
	}

	/**
     * Returns name of page scope attribute under which bundle key will be saved
     *
	 * @return name of page scope attribute under which bundle key will be saved
	 * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Name of page scope attribute under which fieldKey will be saved"
	 */
	public String getKeyVar() {
		return keyVar;
	}

    /**
     * Sets name of page scope attribute under which bundle key will be saved
     *
     * @param keyVar name of page scope attribute under which bundle key will be saved
     */
	public void setKeyVar(String keyVar) {
		this.keyVar = keyVar;
	}

	public void doTag() throws JspException, IOException {

		PageContext pageContext = (PageContext) getJspContext();

		// This tag is only valid when nested within 'grid' tag, so check this
		GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);

		if ( parentGridTag == null ) {
			JspTagException e = new JspTagException("Parent tag is invalid! This tag is only valid when nested within 'grid' tag");
			throw e;
		}

		pageContext.setAttribute(nameVar, fieldName);
		if ( fieldKey != null && fieldKey.length() > 0 ) {
			pageContext.setAttribute(keyVar, fieldKey);
		}

		JspFragment body = getJspBody();
		if ( body != null ) {
			body.invoke(null);
		}
	}

}
